package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.Borrow;
import com.example.bean.History;
import com.example.bean.User;
import com.example.exception.ServiceException;
import com.example.service.HistoryService;
import com.example.service.UserService;
import com.example.until.Result;
import com.example.until.ResultCodeEnum;
import com.example.until.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @title: HistoryController
 * @Author wjf
 * @Date: 2022/10/15 16:17
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @Autowired
    private UserService userService;

    @GetMapping("/page")
    public Result<IPage<History>> getAllUser(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(defaultValue = "") String bookName,
            @RequestParam(value = "nickName", defaultValue = "") String userName) {
        IPage<History> page = new Page<>(current, pageSize);
        System.out.println(bookName);
        System.out.println(userName);
        User user = TokenUtils.getCurrentUser();
        Integer id = null;
        if ("学生".equals(user.getRole())) {
            id = user.getId();
        }
        IPage<History> page1 = historyService.findPage(page, id, bookName, userName);
       //  page1.getRecords().stream().map(m -> m.getNickName()).distinct();
        return Result.ok(page1);
    }

    /**
     * 获取借阅人 下来框数据
     * @return
     */
    @GetMapping("/getUserOptions")
    public Result getUserOptions() {
        List<History> list = historyService.list();
        List<Integer> collect = list.stream().distinct().map(m -> m.getUserId()).collect(Collectors.toList());
        List<User> collect1 = userService.listByIds(collect).stream().map(m -> {
            m.getNickname();
            m.getId();
            return m;
        }).collect(Collectors.toList());
        return Result.ok(collect1);
    }

    @DeleteMapping("/delete/{id}/{type}")
    public Result delete(@PathVariable Integer id,@PathVariable Integer type) {
        try {
            if (type==1){
                History history = new History();
                history.setStatusStu(1);
                history.setId(id);
                historyService.updateById(history);
            }
            if (type==2){
                History history = new History();
                history.setStatusAdm(1);
                history.setId(id);
                historyService.updateById(history);
            }
            return Result.ok();
        } catch (Exception e) {
            throw new ServiceException(ResultCodeEnum.SERVICE_ERROR);
        }
    }

    @DeleteMapping("/delete/batch/{type}/{ids}")
    public Result deleteMore(@PathVariable List<Integer> ids, @PathVariable Integer type){

        try {
            if (type==1){  //普通用户
                for (Integer id:ids) {
                    History history = new History();
                    history.setStatusStu(1);
                    history.setId(id);
                    historyService.updateById(history);
                }
            }
            if (type==2){  //管理员
                for (Integer id:ids) {
                    History history = new History();
                    history.setStatusAdm(1);
                    history.setId(id);
                    historyService.updateById(history);
                }
            }
            return Result.ok();
        } catch (Exception e) {
            throw new ServiceException(ResultCodeEnum.SERVICE_ERROR);
        }
    }

    @PostMapping("/update/status/{borrowId}")
    public Result up(@PathVariable String borrowId){
        LambdaQueryWrapper<History> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(History::getBorrowId,borrowId);
        History history = new History();
        history.setStatusSpk(1);
        historyService.update(history,lambdaQueryWrapper);
        return Result.ok();
    }
}
